package com.example.bookdemo.controller;

import com.example.bookdemo.enums.ResultEnum;
import com.example.bookdemo.model.Result;
import com.example.bookdemo.model.UserInfo;
import com.example.bookdemo.service.UserService;
import com.example.bookdemo.util.JWTUtil;
import com.example.bookdemo.util.SafeUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/user")
@RestController
public class userController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login", produces = "application/json")
    public Result<Object> login(String userName, String password) {
        Assert.hasLength(userName,"用户名为空");
        Assert.hasLength(password,"密码为空");

        // 通过用户名调用密码
        UserInfo userInfo = userService.passwordGet(userName);
        // 判断用户名是否输入正确, 就是直接看调用回来的密码是否为空
        if (userInfo == null || userInfo.getId() < 0) {
            return Result.fail(ResultEnum.NULL_PARAM.getCode(),"用户不存在");
        }
        // 因为前面已经判断为了空, 所以可以对密码进行直接对比
        if (!SafeUtil.inputString(password,userInfo.getPassword())) {
            return Result.fail(ResultEnum.WRONG_PARAM.getCode(),"登录失败, 密码错误");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",userInfo.getId());
        claims.put("userName",userInfo.getUserName());
        String jwt = JWTUtil.getJWT(claims);
        return Result.success(jwt);
    }

    //用户名注册
    @RequestMapping("/register")
    public Boolean register(UserInfo userInfo,String confirmPassword){
        log.info("register 创建新用户:"+userInfo);
        System.out.println(confirmPassword);
        //每一项都不能为空
        Assert.hasLength(userInfo.getUserName(),"用户名不能为空");
        Assert.hasLength(userInfo.getPassword(),"密码不能为空");
        Assert.hasLength(confirmPassword,"输入不能为空");
//        if(!StringUtils.hasLength(userInfo.getUserName())||
//        !StringUtils.hasLength(userInfo.getPassword())){
//            log.error("信息不完全,没办法进行用户创建");
//            return false;
//        }
        String newPassword = SafeUtil.getString(userInfo.getPassword());
        userInfo.setPassword(newPassword);
        Integer result = userService.registerInfo(userInfo);
        try {
            return result>0;
        }catch (Exception e){
            log.error("创建新用户失败");
            return false;
        }
    }

    //获取当前登录人员信息
    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request){
        String userToken = request.getHeader("user_token");
        System.out.println("userToken: "+userToken);
        Integer userId = JWTUtil.getUserToken(userToken);
        Assert.notNull(userId,"用户未登录");
        UserInfo userInfo = userService.selectUserInfo(userId);
        System.out.println(userInfo);
        return userInfo;
    }

    //用户修改个人信息
    @RequestMapping("/updateUserInfo")
    public Boolean updateInfo(UserInfo userInfo){
        System.out.println(userInfo);
        if(userInfo.getId()==null){
            return false;
        }
        if(userInfo.getPassword()!=null){
            log.info("用户设置了新密码,密码: "+userInfo.getPassword());
            String newPassword = SafeUtil.getString(userInfo.getPassword());
            userInfo.setPassword(newPassword);
        }
        Integer result = userService.updateUserInfo(userInfo);
        try {
            return result>0;
        }catch (Exception e){
            log.error("修改用户信息失败");
            return false;
        }
    }
}